<?php

/**
 *  登陆页
 * @file   Login.php
 * @date   2017-11-25
 * @author jim
 */

namespace app\admin\controller;

use app\admin\model\BrJsCompanyModel;
use think\Controller;
use app\admin\model\BrJsAccoutModel;
use app\admin\model\JsAdminLog;

class Login extends Controller {
    /**
     * 登入
     */
    public function index() {
        if (isset($_POST['dosubmit'])) {
            $username = input('post.username');
            $password = input('post.password');

            $param['username']=$username;
            $userModel = new BrJsAccoutModel();
            $resData= $userModel->findData($param);

            if(!$resData['id'] || strtolower($resData['passwd']) != md5($password)){
                $this->redirect('login/index',['tip'=>'账号或密码错误']);
            } elseif($resData['status'] != 1) {
                $this->redirect('login/index',['tip'=>'账号被冻结或删除']);
            } else{
                //查询公司所在城市
                $companyParam['id'] = $resData['cpid'];
                $companyModel = new BrJsCompanyModel();
                $companyData = $companyModel->findData($companyParam);
                if (!$companyData) {
                    $this->redirect('login/index',['tip'=>'公司信息不存在']);
                }

                session('user_id', $resData['id']);
                session('user_name', $resData['username']);
                session('user_realname', $resData['realname']);
                session('user_type', $resData['type']);
                session('company_id',$companyData['Id']);
                session('company_name', $companyData['CompanyName']);
                session('company_type', $companyData['CompanyTypeId']); //,1省2市,3县区,4惠民店
                session('company_create_time',$companyData['CreateOn']);
                session('province_id',$companyData['ProvinceId']);
                session('city_id',$companyData['CityId']);
                session('county_id',$companyData['CountyId']);
                session('town_id',$companyData['TownId']);
                session('allot_model',$companyData['allot_model']);//分红模式 1年 2季度 3月
                session('allot_percent',$companyData['allot_percent']);//分红比例
                $logModel = new JsAdminLog();
                $logModel->log('分公司登录');
                $this->redirect('index/index');
            }
        }
        if (session('user_id')) {
            redirect('index/index');
        }
        $this->assign('tip',request()->param('tip'));
        $this->view->engine->layout(false);
        return $this->fetch('login');
    }

    /**
     * 登出
     */
    public function logout() {
        session(null);
        $this->redirect('login/index');
    }

}
